package com.yanqu.road.server.manager.unioncampwar;

import com.yanqu.road.dao.impl.unioncampwar.*;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.unioncampwar.*;
import com.yanqu.road.entity.unioncampwar.center.CenterUnionCampWarGroupData;
import com.yanqu.road.entity.unioncampwar.cross.CrossUnionCampWarGroupData;
import com.yanqu.road.entity.unioncampwar.cross.CrossUnionCampWarGroupUnionData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionAbilityRankInfo;
import com.yanqu.road.server.manager.union.CrossUnionActivityAbilityRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.unioncampwar.service.*;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.CrossUnionCampWarGroupMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.sync.message.Service;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionCampWarMgr extends TempCrossMgr {
    private static final Object lock = new Object();
    /**
     * key:weekId
     */
    private static Map<Integer, CrossUnionCampWarActivityWeek> activityWeekGroupMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        int thisWeekId = GameConfig.getSystemThisWeekId();
        int endWeekId = 0;
        for (Integer weekId : activityWeekGroupMap.keySet()) {
            if (weekId < thisWeekId) {
                endWeekId = weekId;
                CrossUnionCampWarActivityWeek activityWeek = activityWeekGroupMap.get(weekId);
                for (UnionCampWarActivity warActivity : activityWeek.getActivityList()) {
                    if (warActivity.getCampData().getAutoSend() == 0) {
                        endWeekId = 0;
                        break;
                    }
                }
            }
        }

        if (endWeekId != 0) {
            activityWeekGroupMap.remove(endWeekId);
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        int thisWeekId = GameConfig.getSystemThisWeekId();
        int preWeekId = GameConfig.getSystemPreWeekId();

        Map<Integer, CrossUnionCampWarActivityWeek> tempWeekActivityMap = new ConcurrentHashMap<>();
        CrossUnionCampWarActivityWeek activityWeek = reloadActivityWeek(thisWeekId);

        if (!activityWeek.getActivityList().isEmpty()) {
            tempWeekActivityMap.put(activityWeek.getWeekId(), activityWeek);
        }

        CrossUnionCampWarActivityWeek preActivityWeek = reloadActivityWeek(preWeekId);
        if (!preActivityWeek.getActivityList().isEmpty()) {
            tempWeekActivityMap.put(preActivityWeek.getWeekId(), preActivityWeek);
        }

        activityWeekGroupMap = tempWeekActivityMap;


        return true;
    }

    public CrossUnionCampWarActivityWeek reloadActivityWeek(int thisWeekId) {
        CrossUnionCampWarActivityWeek activityWeek = new CrossUnionCampWarActivityWeek(thisWeekId);

        Map<Integer, Map<Integer, UnionCampWarCampData>> tempCampDataMap = new UnionCampWarCampDaoImpl().getUnionCampWarCampDataMap(thisWeekId);
        Map<Integer, Map<Integer, List<UnionCampWarHoldData>>> tempHoldDataMap = new UnionCampWarHoldDaoImpl().getUnionCampWarHoldDataMap(thisWeekId);
        Map<Integer, Map<Integer, List<UnionCampWarUserData>>> tempUserDataMap = new UnionCampWarUserDaoImpl().getUnionCampWarUserDataMap(thisWeekId);
        Map<Integer, Map<Integer, List<UnionCampWarNoticeData>>> tempNoticeDataMap = new UnionCampWarNoticeDaoImpl().getUnionCampWarNoticeDataMap(thisWeekId);
        Map<Integer, Map<Integer, List<UnionCampWarBoxNoticeData>>> tempBoxNoticeDataMap = new UnionCampWarBoxNoticeDaoImpl().getUnionCampWarBoxNoticeDataMap(thisWeekId);

        for (Map.Entry<Integer, Map<Integer, UnionCampWarCampData>> entry : tempCampDataMap.entrySet()) {
            int centerGroupId = entry.getKey();
            for (Map.Entry<Integer, UnionCampWarCampData> campDataEntry : entry.getValue().entrySet()) {
                int groupId = campDataEntry.getKey();

                List<UnionCampWarHoldData> holdDataList = new ArrayList<>();
                if (tempHoldDataMap.containsKey(centerGroupId) && tempHoldDataMap.get(centerGroupId).containsKey(groupId)) {
                    holdDataList = tempHoldDataMap.get(centerGroupId).get(groupId);
                }

                List<UnionCampWarUserData> userDataList = new ArrayList<>();
                if (tempUserDataMap.containsKey(centerGroupId) && tempUserDataMap.get(centerGroupId).containsKey(groupId)) {
                    userDataList = tempUserDataMap.get(centerGroupId).get(groupId);
                }

                List<UnionCampWarNoticeData> noticeDataList = new ArrayList<>();
                if (tempNoticeDataMap.containsKey(centerGroupId) && tempNoticeDataMap.get(centerGroupId).containsKey(groupId)) {
                    noticeDataList = tempNoticeDataMap.get(centerGroupId).get(groupId);
                }

                List<UnionCampWarBoxNoticeData> boxNoticeDataList = new ArrayList<>();
                if (tempBoxNoticeDataMap.containsKey(centerGroupId) && tempBoxNoticeDataMap.get(centerGroupId).containsKey(groupId)) {
                    boxNoticeDataList = tempBoxNoticeDataMap.get(centerGroupId).get(groupId);
                }

                UnionCampWarActivity campWarActivity = new UnionCampWarActivity(centerGroupId, groupId, thisWeekId);
                campWarActivity.initByDb(campDataEntry.getValue(), holdDataList, userDataList, noticeDataList, boxNoticeDataList);

                campWarActivity.setAllSync(campWarActivity.getNeedSyncPatronsHold().isEmpty());

                activityWeek.addActivity(campWarActivity);
            }
        }

        return activityWeek;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        List<CrossUnionCampWarActivityWeek> activityWeekList = new ArrayList<>(activityWeekGroupMap.values());
        for (CrossUnionCampWarActivityWeek activityWeek : activityWeekList) {
            activityWeek.save();
        }

        int thisWeekId = GameConfig.getSystemThisWeekId();
        int endWeekId = 0;
        for (Integer weekId : activityWeekGroupMap.keySet()) {
            if (weekId < thisWeekId) {
                endWeekId = weekId;
                CrossUnionCampWarActivityWeek activityWeek = activityWeekGroupMap.get(weekId);
                for (UnionCampWarActivity warActivity : activityWeek.getActivityList()) {
                    if (warActivity.getCampData().getAutoSend() == 0) {
                        endWeekId = 0;
                        break;
                    }
                }
            }
        }

        if (endWeekId != 0) {
            activityWeekGroupMap.remove(endWeekId);
        }
        return true;
    }

    public static void initService(Service syncMessageService) {
        syncMessageService.addCrossServiceCommand(CrossProtocol.C3_UNION_CAMP_WAR_GET_REWARD, new CrossUnionCampWarGetRankRewardCmd());
        syncMessageService.addCrossServiceCommand(CrossProtocol.C3_UNION_CAMP_WAR_WORSHIP, new CrossUnionCampWarWorshipCmd());
    }

    public static CrossUnionCampWarActivityGroup getActivityGroup(int centerGroupId, boolean newIfnull) {
        int weekId = GameConfig.getSystemThisWeekId();;
        return getActivityGroup(centerGroupId, weekId, newIfnull);
    }

    public static CrossUnionCampWarActivityGroup getActivityGroup(int centerGroupId, int weekId, boolean newIfnull) {
        CrossUnionCampWarActivityWeek activityWeek = activityWeekGroupMap.get(weekId);
        if (null == activityWeek && newIfnull) {
            synchronized (activityWeekGroupMap) {
                activityWeek = activityWeekGroupMap.get(weekId);
                if (null == activityWeek) {
                    activityWeek = new CrossUnionCampWarActivityWeek(weekId);
                    activityWeekGroupMap.put(weekId, activityWeek);
                }
            }
        }
        if (activityWeek == null) {
            return null;
        }
        return activityWeek.getActivityGroup(centerGroupId, newIfnull);
    }

    /**
     * 报名期截止前5分钟，检查所有分组有没有创建warActivity,没有就创建,创建后检查有没有同步完门客数据
     */
    public static void checkActivityGroupTask() {
        if (!ConfigMgr.getUnionCampWarConfig().isRegistrationEndTime()) {
            return;
        }
        int weekId = GameConfig.getSystemThisWeekId();
        List<CenterUnionCampWarGroupData> groupList = CrossUnionCampWarGroupMgr.getServerGroupList();
        for (CenterUnionCampWarGroupData centerGroupData : groupList) {
            Map<Integer, CrossUnionCampWarGroupData> groupDataMap = CrossUnionCampWarLockGroupMgr.getGroupDataMap(centerGroupData.getGroupId());
            if (null == groupDataMap) {
                continue;
            }

            if (centerGroupData.getServerSet().isEmpty()){
                continue;
            }

            CrossUnionCampWarActivityGroup activityGroup = getActivityGroup(centerGroupData.getGroupId(), weekId, true);
            if (!activityGroup.getActivityList().isEmpty()){
                continue;
            }
            for (CrossUnionCampWarGroupData groupData : groupDataMap.values()) {
                getLogger().info("商帮开始生成战场数据,weekId={},centerGroupId={},groupId={}", weekId, centerGroupData.getGroupId(), groupData.getGroupId());
                //锁定成员名单
                for (CrossUnionCampWarGroupUnionData groupUnionData : groupData.getUnionMap().values()) {
                    Map<Long, Integer> memberPositionMap = CrossUnionMgr.getMemberPositionMap(groupUnionData.getUnionId());
                    groupUnionData.setMermberPositionMap(memberPositionMap);
                    for (Long memberId : memberPositionMap.keySet()) {
                        //建立玩家id的索引
                        CrossUnionCampWarLockGroupMgr.putGroupUnionData(memberId, groupUnionData);
                    }
                }

                UnionCampWarActivity warActivity = new UnionCampWarActivity(centerGroupData.getGroupId(), groupData.getGroupId(), weekId);
                UnionCampWarCampData warCampData = new UnionCampWarCampData(centerGroupData.getGroupId(), groupData.getGroupId(), weekId);
                warCampData.setRouteListMap(ConfigMgr.getUnionCampWarConfig().getRoteHoldList());
                warActivity.setCampData(warCampData);

                int worshipInitNum1 = ConfigMgr.getUnionCampWarConfig().getWorshipInitNum();
                int worshipInitNum2 = ConfigMgr.getUnionCampWarConfig().getWorshipInitNum();
                List<Integer> worshipList =new ArrayList<>();
                worshipList.add(worshipInitNum1);
                worshipList.add(worshipInitNum2);
                warCampData.setWorshipList(worshipList);

                for (int campId = 1; campId <= 2; campId++) {
                    CrossUnionAbilityRankInfo rankInfo = CrossUnionActivityAbilityRankMgr.getUnionCampWarGroupRankInfo(centerGroupData.getGroupId(), groupData.getGroupId(), campId);
                    if (null == rankInfo) {
                        break;
                    }
                    UnionCampWarCampInfo campInfo = warActivity.getCampInfo(campId);
                    if (null == campInfo) {
                        campInfo = new UnionCampWarCampInfo(campId);
                        warActivity.addCampInfo(campInfo);
                    }
                    //取前N的商会创建据点
                    int topNeed = ConfigMgr.getUnionCampWarConfig().getTopUnionCount();
                    for (int i = 0; i < rankInfo.getRankMsg().size() && i < topNeed; i++) {
                        RankProto.UnionRankTempMsg unionRankTempMsg = rankInfo.getRankMsg().get(i);
                        int holdId = i + 1;
                        //创建据点数据
                        UnionCampWarHoldData holdData = new UnionCampWarHoldData(warActivity.getCenterGroupId(), warActivity.getGroupId(), campId, holdId, unionRankTempMsg.getUnionUid(), warCampData.getWeekId());
                        holdData.setTotalHp(ConfigMgr.getUnionCampWarConfig().getHoldHp(holdId));
                        holdData.setLeftHp(holdData.getTotalHp());
                        warActivity.addHoldData(holdData);
                    }
                    campInfo.initCampHp();
                }
                activityGroup.addActivity(warActivity);
            }
        }
    }

    /**
     * 检查warActivity里的据点门客数量都同步了没,没有就找游戏服要
     */
    public static void syncPatronsTask() {
        List<CrossUnionCampWarActivityWeek> list = new ArrayList<>(activityWeekGroupMap.values());
        for (CrossUnionCampWarActivityWeek activityGroupWeek : list) {
            for (UnionCampWarActivity warActivity : activityGroupWeek.getActivityList()) {
                if (warActivity.isAllSync()) {
                    continue;
                }
                List<UnionCampWarHoldData> needSyncPatronsHold = warActivity.getNeedSyncPatronsHold();
                if (needSyncPatronsHold.isEmpty()) {
                    warActivity.setAllSync(true);
                    CrossUnionCampWarLockGroupMgr.calcHoldHpConfig(warActivity);
                    continue;
                }
                getLogger().info("weekId={},centerGroupId={},groupId={}", warActivity.getWeekId(), warActivity.getCenterGroupId(), warActivity.getGroupId());
                for (UnionCampWarHoldData warHoldData : needSyncPatronsHold) {
                    CrossUnionCampWarGroupData groupData = CrossUnionCampWarLockGroupMgr.getGroupData(warHoldData.getCenterGroupId(), warHoldData.getGroupId());
                    if (null == groupData) {
                        continue;
                    }
                    CrossUnionCampWarGroupUnionData groupUnionData = groupData.getUnionMap().get(warHoldData.getUnionId());
                    if (null == groupUnionData) {
                        continue;
                    }
                    getLogger().info("商帮请求同步门客数据,weekId={},centerGroupId={},groupId={},unionId={},holdId={}",
                            groupUnionData.getWeekId(), groupUnionData.getCenterGroupId(), groupUnionData.getGroupId(), groupUnionData.getUnionId(), warHoldData.getHoldId());
                    UnionCampWar.CrossUnionCampWarPatronsNumReqMsg.Builder reqMsg = UnionCampWar.CrossUnionCampWarPatronsNumReqMsg.newBuilder();
                    reqMsg.setCenterGroupId(warHoldData.getCenterGroupId()).setGroupId(warHoldData.getGroupId()).setUnionId(groupUnionData.getUnionId());
                    reqMsg.addAllMemberId(groupUnionData.getMermberPositionMap().keySet());
                    MessageHelper.sendPacket(groupUnionData.getServerId(), 0,
                            YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CAMP_WAR_PATRONS_NUM_CROSS_REQ, reqMsg));
                }
            }
        }
    }

    /**
     * 接收门客数消息
     */
    public static void receivePatronsNumMsg(UnionCampWar.CrossUnionCampWarPatronsNumRespMsg respMsg) {
        UnionCampWarActivity warActivity = getWarActivity(respMsg.getCenterGroupId(), respMsg.getGroupId());
        if (null == warActivity) {
            return;
        }
        warActivity.updateHoldPatronsNum(respMsg.getUnionId(), respMsg.getPatronsNum());
        UnionCampWarHoldData holdData = warActivity.getHoldData(respMsg.getUnionId());
        int holdId = 0;
        if (holdData != null) {
            holdId = holdData.getHoldId();
        }
        getLogger().info("商帮收到门客数,centerGroupId={},groupId={},unionId={},patronsNum={},holdId={}", respMsg.getCenterGroupId(), respMsg.getGroupId(), respMsg.getUnionId(), respMsg.getPatronsNum(), holdId);
    }

    /**
     * 检查结束
     */
    public static void checkWarEndTask() {
        if (!ConfigMgr.getUnionCampWarConfig().isInRewardTime()) {
            return;
        }
        List<CrossUnionCampWarActivityWeek> list = new ArrayList<>(activityWeekGroupMap.values());
        for (CrossUnionCampWarActivityWeek activityGroupWeek : list) {
            for (UnionCampWarActivity warActivity : activityGroupWeek.getActivityList()) {
                if (warActivity.getCampData().getEndTime() != 0) {
                    continue;
                }

                warActivity.warEnd();
                getLogger().info("商帮战场结束,centerGroupId={},groupId={}", warActivity.getCenterGroupId(), warActivity.getGroupId());
            }
        }
    }

    /**
     * 榜单补发任务
     */
    public static void autoSendRankRewardTask() {
        if (!ConfigMgr.getUnionCampWarConfig().isAutoRankRewardTime()) {
            return;
        }

        List<CrossUnionCampWarActivityWeek> list = new ArrayList<>(activityWeekGroupMap.values());
        for (CrossUnionCampWarActivityWeek activityGroupWeek : list) {
            for (UnionCampWarActivity warActivity : activityGroupWeek.getActivityList()) {
                if (warActivity.getCampData().getAutoSend() != 0 || warActivity.getCampData().getWinCampId() == 0) {
                    continue;
                }

                getLogger().info("商帮个人榜单补发,weekId={},centerGroupId={},groupId={}", warActivity.getWeekId(), warActivity.getCenterGroupId(), warActivity.getGroupId());
                Map<Long, UnionCampWar.CrossUnionCampWarAutoSendRewardMsg.Builder> sendRewardMsgMap = warActivity.autoSendReward();

                for (Map.Entry<Long, UnionCampWar.CrossUnionCampWarAutoSendRewardMsg.Builder> entry : sendRewardMsgMap.entrySet()) {
                    long serverId = entry.getKey();
                    MessageHelper.sendPacket(serverId, 0,
                            YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CAMP_WAR_AUTO_SEND_RANK_REWARD, entry.getValue()));
                }
            }
        }
    }


    /**
     * 参与区服
     */
    public static UnionCampWar.UnionCampWarServerListRspMsg.Builder getServerListMsg(long serverId, long userId) {
        UnionCampWar.UnionCampWarServerListRspMsg.Builder builder = UnionCampWar.UnionCampWarServerListRspMsg.newBuilder();
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (null == unionMember) {
            return builder;
        }
        int campId = 0;
        if (ConfigMgr.getUnionCampWarConfig().isInRegistrationTime()) {//报名期
            String unionId = unionMember.getUnionUid();
            CrossUnionCampWarGroupData groupData = CrossUnionCampWarLockGroupMgr.getGroupData(serverId, unionId);
            if (null == groupData) {
                return builder;
            }
            CrossUnionCampWarGroupUnionData groupUnionData = groupData.getGroupUnionData(unionMember.getUnionUid());
            if (groupUnionData != null) {
                campId = groupUnionData.getCampId();
            }

            builder.addAllServerId(groupData.getServerSet());
        } else {//报名结束
            CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
            if (null == groupUnionData) {
                return builder;
            }

            CrossUnionCampWarGroupData groupData = CrossUnionCampWarLockGroupMgr.getGroupData(groupUnionData.getCenterGroupId(), groupUnionData.getGroupId());
            if (null == groupData) {
                return builder;
            }

            campId = groupUnionData.getCampId();

            builder.addAllServerId(groupData.getServerSet());
            UnionCampWarActivity warActivity = getWarActivity(groupData.getCenterGroupId(), groupData.getGroupId());
            if (null != warActivity) {
                builder.setIsHoldAlive(warActivity.isEnemyHoldAlive(groupUnionData.getCampId()));
            }
        }

        builder.setCampId(campId);

        return builder;
    }

    /**
     * 获取后台配置
     */
    public static UnionCampWar.UnionCampWarConfigRespMsg.Builder getConfigMsg(long serverId, long userId) {
        UnionCampWar.UnionCampWarConfigRespMsg.Builder builder = UnionCampWar.UnionCampWarConfigRespMsg.newBuilder();
        builder.setRet(0);
        int centerGroupId = 1;
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (null != unionMember) {
            if (!ConfigMgr.getUnionCampWarConfig().isRegistrationEndTime()) {
                String unionId = unionMember.getUnionUid();
                CrossUnionCampWarGroupData groupData = CrossUnionCampWarLockGroupMgr.getGroupData(serverId, unionId);
                if (null != groupData) {
                    centerGroupId = groupData.getCenterGroupId();
                }
            } else {
                CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
                if (null != groupUnionData) {
                    centerGroupId = groupUnionData.getCenterGroupId();
                }
            }
        }

        builder.addAllConfig(CrossUnionCampWarGroupMgr.getConfigListMsg(centerGroupId));

        return builder;
    }

    /**
     * 报名期榜单
     */
    public static UnionCampWar.UnionCampWarRegistrationRankListRespMsg.Builder getRegistrationRankListMsg(long serverId, int campId, long userId) {
        UnionCampWar.UnionCampWarRegistrationRankListRespMsg.Builder builder = UnionCampWar.UnionCampWarRegistrationRankListRespMsg.newBuilder();
        builder.setCampId(campId);
        String unionId = "";
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (null != unionMember) {
            unionId = unionMember.getUnionUid();
        }

        CenterUnionCampWarGroupData centerGroupData = CrossUnionCampWarGroupMgr.getServerGroupByServerId(serverId);
        if (null == centerGroupData) {
            return builder;
        }

        int groupId = CrossUnionCampWarLockGroupMgr.getGroupId(serverId, unionId);
        if (0 == groupId) {
            //不在分组的商会看不了
            return builder;
        }

        CrossUnionAbilityRankInfo rankInfo = CrossUnionActivityAbilityRankMgr.getUnionCampWarGroupRankInfo(centerGroupData.getGroupId(), groupId, campId);
        if (null == rankInfo) {
            return builder;
        }
        rankInfo.setEndTime(ConfigMgr.getUnionCampWarConfig().getNowWeekBattleTime());
        builder.addAllRankList(rankInfo.getRankMsg());
        int rank = 0;
        int myRank = -1;
        String myValue = BigInteger.ZERO.toString();
        for (RankProto.UnionRankTempMsg unionRankTempMsg : builder.getRankListList()) {
            rank++;
            if (unionRankTempMsg.getUnionUid().equals(unionId)) {
                myRank = rank;
                myValue = unionRankTempMsg.getValue();
            }
        }
        if (myRank == -1) {
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
            if (null != unionInfo) {
                myValue = unionInfo.getTotalMaxAbility().toString();
            }
        }
        builder.setMyRank(myRank);
        builder.setMyValue(myValue);

        return builder;
    }

    public static UnionCampWarActivity getWarActivity(int centerGroupId, int groupId) {
        CrossUnionCampWarActivityGroup activityGroup = getActivityGroup(centerGroupId, false);
        if (null != activityGroup) {
            return activityGroup.getActivity(groupId);
        }
        return null;
    }

    public static UnionCampWarActivity getWarActivity(String unionId) {
        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
        if (null == unionInfo) {
            return null;
        }
        int centerGroupId = CrossUnionCampWarGroupMgr.getGroupIdByServerId(unionInfo.getServerId());
        int groupId = CrossUnionCampWarLockGroupMgr.getGroupId(unionInfo.getServerId(), unionId);
        return getWarActivity(centerGroupId, groupId);
    }

    public static UnionCampWarActivity getWarActivity(long userId) {
        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return null;
        }
        return getWarActivity(groupUnionData);
    }

    public static UnionCampWarActivity getWarActivity(CrossUnionCampWarGroupUnionData groupUnionData) {
        if (null == groupUnionData) {
            return null;
        }
        return getWarActivity(groupUnionData.getCenterGroupId(), groupUnionData.getGroupId());
    }

    public static UnionCampWarUserData getWarUserData(long userId) {
        UnionCampWarActivity campWarActivity = getWarActivity(userId);
        if (null == campWarActivity) {
            return null;
        }
        return campWarActivity.getWarUserData(userId);
    }

    /**
     * 主界面数据
     */
    public static UnionCampWar.UnionCampWarMainScenceRespMsg.Builder getMainScenceMsg(long userId, long lastEnterTime) {
        UnionCampWar.UnionCampWarMainScenceRespMsg.Builder msg = UnionCampWar.UnionCampWarMainScenceRespMsg.newBuilder();
        msg.setRet(0);
//        if (!ConfigMgr.getUnionCampWarConfig().isInBattleTime()) {
//            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_BATTLE_TIME);
//        }

        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarActivity campWarActivity = getWarActivity(groupUnionData);
        if (null == campWarActivity) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_BATTLE_TIME);
        }

        msg = campWarActivity.getMainScenceMsg(userId);
        msg.setOwnCampId(groupUnionData.getCampId());
        msg.setLastEnterTime(lastEnterTime);
        return msg;
    }

    /**
     * 进攻
     */
    public static int attackHold(long userId, long serverId, UnionCampWar.CrossUnionCampWarAttackReqMsg reqMsg) {

        if (!ConfigMgr.getUnionCampWarConfig().isInBattleTime()) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_BATTLE_TIME;
        }

        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (null == unionMember) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION;
        }

        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION;
        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION;
        }

        if (!warActivity.isAllSync()) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_ALL_SYNC;
        }

        UnionCampWarUserData attackUserData = warActivity.getAndNewWarUserData(userId, groupUnionData.getCampId(), serverId);
        if (null == attackUserData) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION;
        }

        if (reqMsg.getClientReq().getCampId() == attackUserData.getCampId()) {
            return GameErrorCode.E_UNION_CAMP_WAR_SAME_CAMP;
        }

        UnionCampWarCampInfo attackCampInfo = warActivity.getCampInfo(attackUserData.getCampId());
        if (null == attackCampInfo) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO;
        }

        UnionCampWarCampInfo defendCampInfo = warActivity.getCampInfo(reqMsg.getClientReq().getCampId());
        if (null == defendCampInfo) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO;
        }

        if (defendCampInfo.getLeftHp() <= 0) {
            return GameErrorCode.E_UNION_CAMP_WAR_CAMP_LEFT_HP_ZERO;
        }

        UnionCampWarHoldData holdInfo = defendCampInfo.getHoldInfo(reqMsg.getClientReq().getHoldId());
        if (null == holdInfo) {
            return GameErrorCode.E_UNION_CAMP_WAR_NOT_HOLD_INFO;
        }

        if (holdInfo.getLeftHp() <= 0) {
            return GameErrorCode.E_UNION_CAMP_WAR_HOLD_LEFT_HP_ZERO;
        }

        if (attackUserData.getAttackTimes() == -1) {
            attackUserData.setAttackTimes(reqMsg.getNowAttackTimes() + 1);
        }
        if (attackUserData.getAttackTimes() == reqMsg.getNowAttackTimes()) {
            return GameErrorCode.E_UNION_CAMP_WAR_USER_NOT_ATTACK_TIMES;
        }

        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(holdInfo.getUnionId());
        if (null == unionInfo) {
            return GameErrorCode.E_ERROR_DATA;
        }

        if (!warActivity.canAttackHold(defendCampInfo.getCampId(), holdInfo.getHoldId())) {
            return GameErrorCode.E_UNION_CAMP_WAR_ROUTE_NOT_ENOUGH;
        }

        UnionCampWar.CrossUnionCampWarToGameAttackReqMsg.Builder attackReqMsg = UnionCampWar.CrossUnionCampWarToGameAttackReqMsg.newBuilder();
        attackReqMsg.setUserId(userId);
        attackReqMsg.setTargetUnionId(unionInfo.getUnionUid());
        attackReqMsg.addAllPatronsInfo(reqMsg.getPatronsInfoList());
        attackReqMsg.setBuffId(reqMsg.getClientReq().getBuffId());
        for (BigDecimal ability : warActivity.getCampData().getAvgAbilityList()) {
            attackReqMsg.addAvgAbility(ability.toString());
        }
        int attackType = reqMsg.getClientReq().getAttackType();
        UnionCampWar.CrossUnionCampWarToGameAttackRespMsg.Builder attackRespMsg = UnionCampWar.CrossUnionCampWarToGameAttackRespMsg.newBuilder();
        CompletableFuture<SyncResponse> future = MessageHelper.sendToGameServer(unionInfo.getServerId(), GameProtocol.S_UNION_CAMP_WAR_ATTACK_TEAM_CROSS_REQ, -1, 0, attackReqMsg, attackRespMsg);
        future.thenAccept(syncResponse -> {
            attackUserData.setAttackTimes(-1);
            attackUserData.setLastAttackTime(DateHelper.getCurrentTime());
            UnionCampWar.UnionCampWarAttackRespMsg.Builder msg = UnionCampWar.UnionCampWarAttackRespMsg.newBuilder();
            msg.setRet(0);

            if (syncResponse.getErrorCode() != 0) {
                getLogger().info("商帮战斗请求失败,错误{},进攻方{},防守商会{}", syncResponse.getErrorCode(), attackReqMsg.getUserId(), attackReqMsg.getTargetUnionId());
                msg.setRet(syncResponse.getErrorCode());
                MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CAMP_WAR_ATTACK_TEAM_CROSS_BACK, msg));
                return;
            }

            int defeatNum = attackRespMsg.getDefeatNum();
            int scoreAdd = ConfigMgr.getUnionCampWarConfig().getWinAddUserScore(holdInfo.getHoldId(), defeatNum);
            int subHp = ConfigMgr.getUnionCampWarConfig().getDefeatNumSubHp(holdInfo.getHoldId(), defeatNum);
            int realSubHp = 0;
            int leftHp = holdInfo.getLeftHp();
            synchronized (holdInfo) {
                if (holdInfo.getLeftHp() <= 0) {
                    msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_HOLD_LEFT_HP_ZERO);
                    MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CAMP_WAR_ATTACK_TEAM_CROSS_BACK, msg));
                    return;
                }

                realSubHp = holdInfo.subHp(subHp);
                leftHp = holdInfo.getLeftHp();

                attackUserData.setScore(attackUserData.getScore() + scoreAdd);

                attackUserData.addAttackDamage(holdInfo.getHoldId(), realSubHp);

                holdInfo.addDamage(userId, realSubHp);
            }

            long timeNow = DateHelper.getCurrentTime();

            //更新阵营血量
            defendCampInfo.initCampHp();

            attackCampInfo.updateLastWinTime(timeNow);

            UnionCampWar.UnionCampWarAttackResultTemp.Builder resultMsg = UnionCampWar.UnionCampWarAttackResultTemp.newBuilder();
            resultMsg.setIsWin(attackRespMsg.getIsWin());
            resultMsg.addAllPatronsIdList(attackRespMsg.getPatronsIdListList());
            resultMsg.addAllEnemyPatronsList(attackRespMsg.getEnemyPatronsListList());
            resultMsg.setBattleDetailList(attackRespMsg.getBattleDetailList());
            resultMsg.setDefeatNum(defeatNum);
            resultMsg.setRewards(ConfigMgr.getUnionCampWarConfig().getBattleRewards(holdInfo.getCenterGroupId(), holdInfo.getHoldId(), defeatNum));
            resultMsg.setScoreAdd(scoreAdd);
            resultMsg.setBuffId(attackReqMsg.getBuffId());
            resultMsg.setSubHp(realSubHp);
            resultMsg.setLeftHp(leftHp);
            resultMsg.setHoldId(holdInfo.getHoldId());
            resultMsg.setOwnCampId(attackCampInfo.getCampId());
            resultMsg.setEnemyCampId(defendCampInfo.getCampId());
            resultMsg.setGroupId(holdInfo.getGroupId());
            msg.setResult(resultMsg);
            msg.setAttackType(attackType);

            //发到游戏服结算奖励等数据
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CAMP_WAR_ATTACK_TEAM_CROSS_BACK, msg));

            if (defeatNum >= ConfigMgr.getUnionCampWarConfig().getNoticeNeedDefeatNum()) {
                //公告,跑马灯
                UnionCampWarNoticeData noticeData = new UnionCampWarNoticeData(holdInfo.getCenterGroupId(), holdInfo.getGroupId(), holdInfo.getWeekId());
                noticeData.setWinNum(defeatNum);
                noticeData.setHoldId(holdInfo.getHoldId());
                noticeData.setScoreAdd(scoreAdd);
                noticeData.setAttackCampId(attackUserData.getCampId());
                noticeData.setDefendCampId(defendCampInfo.getCampId());
                noticeData.setAttackUserId(userId);
                noticeData.setDefendUnionId(unionInfo.getUnionUid());
                noticeData.setDefendUnionName(unionInfo.getUnionName());
                noticeData.setBattleTime(timeNow);
                noticeData.setInsertOption();

                UnionCampWar.UnionCampWarMarqueeInfoTemp.Builder noticeMsg = warActivity.addNoticeData(noticeData);
                UnionCampWar.UnionCampWarBattleNoticeAddMsg.Builder broadCastMsg = UnionCampWar.UnionCampWarBattleNoticeAddMsg.newBuilder();
                broadCastMsg.setNoticeMsg(noticeMsg);

                CrossUnionCampWarLockGroupMgr.broadcastServerMsg(holdInfo.getCenterGroupId(), holdInfo.getGroupId(),
                        YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_CAMP_WAR_BATTLE_NOTICE_ADD, broadCastMsg));

            }

            //检查胜负
            synchronized (warActivity) {
                warActivity.winnerCheck();
            }

        });

        return 0;
    }

    /**
     * 查看据点战报,20230628策划改需求,废弃了
     */
    @Deprecated
    public static UnionCampWar.UnionCampWarHoldReportRespMsg.Builder getHoldDefendReportMsg(long userId, int campId, int holdId) {
        UnionCampWar.UnionCampWarHoldReportRespMsg.Builder builder = UnionCampWar.UnionCampWarHoldReportRespMsg.newBuilder();
        builder.setRet(0);
//        UnionCampWarActivity warActivity = getWarActivity(userId);
//        if (null == warActivity) {
//            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
//        }
//
//        UnionCampWarCampInfo campInfo = warActivity.getCampInfo(campId);
//        if (null == campInfo) {
//            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO);
//        }
//
//        UnionCampWarHoldData holdInfo = campInfo.getHoldInfo(holdId);
//        if (null == holdInfo) {
//            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_HOLD_INFO);
//        }
//
//        for (String unionId : holdInfo.getDamageMap().keySet()) {
//            UnionCampWar.UnionCampWarHoldReportTemp.Builder reportMsg = UnionCampWar.UnionCampWarHoldReportTemp.newBuilder();
//            reportMsg.setUnionId(unionId);
//            int unionDamage = holdInfo.getUnionDamage(unionId);
//            if (unionDamage == 0) {
//                continue;
//            }
//            reportMsg.setDamage(holdInfo.getUnionDamage(unionId));
//            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
//            if (null != unionInfo) {
//                reportMsg.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
//            }
//            builder.addReportInfo(reportMsg);
//        }

        return builder;
    }

    /**
     * 查看玩家战报,20230628策划改需求,targetUnionId废弃了,取据点所有玩家伤害
     */
    public static UnionCampWar.UnionCampWarPersonalReportRespMsg.Builder getHoldDefendDetailMsg(long userId, int campId, int holdId) {
        UnionCampWar.UnionCampWarPersonalReportRespMsg.Builder builder = UnionCampWar.UnionCampWarPersonalReportRespMsg.newBuilder();
        builder.setRet(0);
        UnionCampWarActivity warActivity = getWarActivity(userId);
        if (null == warActivity) {
            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarCampInfo campInfo = warActivity.getCampInfo(campId);
        if (null == campInfo) {
            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO);
        }

        UnionCampWarHoldData holdInfo = campInfo.getHoldInfo(holdId);
        if (null == holdInfo) {
            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_HOLD_INFO);
        }
        Map<Long, Integer> damageMap;
        synchronized (holdInfo) {
            damageMap = new HashMap<>(holdInfo.getDamageMap());
        }

        for (Map.Entry<Long, Integer> entry : damageMap.entrySet()) {
            long attackUserId = entry.getKey();
            int damage = entry.getValue();
            if (0 == damage) {
                continue;
            }

            UnionCampWar.UnionCampWarHoldReportDetailTemp.Builder detailMsg = UnionCampWar.UnionCampWarHoldReportDetailTemp.newBuilder();
            detailMsg.setDamage(damage);
            detailMsg.setUserId(attackUserId);
            CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(attackUserId);
            if (null != unionMember) {
                detailMsg.setServerId(unionMember.getServerId());
                PlayerProto.PlayerBaseTempMsg.Builder baseTempMsg = PlayerBasePb.parsePlayerBaseTempMsg(unionMember.getUserBaseInfo());
                detailMsg.setUserBaseInfo(baseTempMsg);
            }
            builder.addReportInfo(detailMsg);
        }

        return builder;
    }

    public static UnionCampWar.UnionCampWarUserScoreRankRespMsg.Builder getCampMemberRankListMsg(long userId, int campId) {
        UnionCampWar.UnionCampWarUserScoreRankRespMsg.Builder builder = UnionCampWar.UnionCampWarUserScoreRankRespMsg.newBuilder();
        builder.setMyRank(-1);
        builder.setMyScore(0);
        builder.setCampId(campId);
        UnionCampWarActivity warActivity = getWarActivity(userId);
        if (null == warActivity) {
            return builder;
        }

        UnionCampWarCampInfo campInfo = warActivity.getCampInfo(campId);
        if (null == campInfo) {
            return builder;
        }
        List<UnionCampWar.UnionCampWarUserScoreRankTemp> rankMsgList = campInfo.getRankMsgList();
        if (rankMsgList.isEmpty()) {
            List<UnionCampWar.UnionCampWarUserScoreRankTemp> newMsgList = new ArrayList<>();
            List<UnionCampWarUserData> scoreRankList = campInfo.getCampMemberScoreRankList();
            for (int i = 0; i < scoreRankList.size(); i++) {
                int rank = i + 1;
                UnionCampWarUserData warUserData = scoreRankList.get(i);
                CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(warUserData.getUserId());
                if (null == unionMember) {
                    continue;
                }
                UnionCampWar.UnionCampWarUserScoreRankTemp.Builder rankMsg = UnionCampWar.UnionCampWarUserScoreRankTemp.newBuilder();
                rankMsg.setRank(rank);
                rankMsg.setScore(warUserData.getScore());
                rankMsg.setUserId(warUserData.getUserId());
                rankMsg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(unionMember.getUserBaseInfo()));
                newMsgList.add(rankMsg.build());
            }
            campInfo.setRankMsgList(newMsgList);
            rankMsgList = campInfo.getRankMsgList();
        }
        builder.addAllRankInfo(rankMsgList);
        for (int i = 0; i < rankMsgList.size(); i++) {
            UnionCampWar.UnionCampWarUserScoreRankTemp rankTemp = rankMsgList.get(i);
            if (userId == rankTemp.getUserId()) {
                builder.setMyRank(rankTemp.getRank());
                builder.setMyScore(rankTemp.getScore());
                break;
            }
        }

        builder.setWorship(warActivity.getWorshipCount(campId));

        return builder;
    }

    /**
     * 宝库界面
     */
    public static UnionCampWar.UnionCampBoxScenceRespMsg.Builder getCampBoxScence(long userId) {
        UnionCampWar.UnionCampBoxScenceRespMsg.Builder msg = UnionCampWar.UnionCampBoxScenceRespMsg.newBuilder();
        msg.setRet(0);

//        if (!ConfigMgr.getUnionCampWarConfig().isInRewardTime()) {
//            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_REWARD_TIME);
//        }

        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarActivity campWarActivity = getWarActivity(groupUnionData);
        if (null == campWarActivity) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO);
        }

        UnionCampWarCampInfo campInfo = campWarActivity.getCampInfo(groupUnionData.getCampId());
        if (null == campInfo) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO);
        }

        campWarActivity.initCampBox();

        campWarActivity.parseCampBoxScence(campInfo.getCampId(), msg);

        UnionCampWar.UnionCampBoxNoticeTemp lastNoticeMsg = campInfo.getLastBoxNoticeMsg();
        if (null != lastNoticeMsg) {
            msg.setLastBoxNotice(lastNoticeMsg);
        }


        return msg;
    }

    /**
     * 宝库抽奖
     */
    public static UnionCampWar.UnionCampWarOpenBoxRespMsg.Builder openBox(long userId, int boxId) {
        UnionCampWar.UnionCampWarOpenBoxRespMsg.Builder msg = UnionCampWar.UnionCampWarOpenBoxRespMsg.newBuilder();
        msg.setRet(0);
//        if (!ConfigMgr.getUnionCampWarConfig().isInRewardTime()) {
//            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_REWARD_TIME);
//        }

        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (null == unionMember) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarCampInfo campInfo = warActivity.getCampInfo(groupUnionData.getCampId());
        if (null == campInfo) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarCampData campDataData = warActivity.getCampData();
        if (null == campDataData) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO);
        }
        UnionCampWar.UnionCampWarBoxBigNoticeMsg.Builder broadcastMsg = UnionCampWar.UnionCampWarBoxBigNoticeMsg.newBuilder();

        synchronized (campInfo) {
            UnionCampWarOpenBoxInfo openBoxInfo = null;
            int boxRound = 0;
            int bigBoxId = 0;
            if (groupUnionData.getCampId() == 1) {
                if (campDataData.getLeftOpenBoxInfoMap().containsKey(boxId)) {
                    return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_BOX_ALREADY_OPEN);
                }
                boxRound = campDataData.getLeftBoxRound();
                bigBoxId = campDataData.getLeftBigBoxId();
            } else {
                if (campDataData.getRightOpenBoxInfoMap().containsKey(boxId)) {
                    return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_BOX_ALREADY_OPEN);
                }
                boxRound = campDataData.getRightBoxRound();
                bigBoxId = campDataData.getRightBigBoxId();
            }

            if (boxId == bigBoxId) {//开到大奖
                openBoxInfo = new UnionCampWarOpenBoxInfo(userId, warActivity.getCampBoxBigRewards(campInfo.getCampId()));
                campDataData.putOpenBoxInfo(boxId, campInfo.getCampId(), openBoxInfo);
                msg.setIsBigReward(true);
                //加抽奖公告
                UnionCampWarBoxNoticeData boxNoticeData = new UnionCampWarBoxNoticeData(
                        campDataData.getCenterGroupId(),
                        campDataData.getGroupId(),
                        campDataData.getWeekId(),
                        campInfo.getCampId()
                );
                boxNoticeData.setUserId(userId);
                boxNoticeData.setRewards(openBoxInfo.getRewards());
                boxNoticeData.setSererId(unionMember.getServerId());
                boxNoticeData.setUserName(unionMember.getUserBaseInfo().getNickName());
                boxNoticeData.setOpenTime(DateHelper.getCurrentTime());

                UnionCampWar.UnionCampBoxNoticeTemp noticeTemp = warActivity.addBoxNoticeData(boxNoticeData);

                //下一轮
                warActivity.nextBoxRound(campInfo.getCampId());

                broadcastMsg.setNotice(noticeTemp);
            } else {
                Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(ConfigMgr.getUnionCampWarConfig().getGeneralLotteryList(campDataData.getCenterGroupId()));
                openBoxInfo = new UnionCampWarOpenBoxInfo(userId, PropertyHelper.parsePropertyToString(randomReward));
                campDataData.putOpenBoxInfo(boxId, campInfo.getCampId(), openBoxInfo);
                msg.setIsBigReward(false);
            }
            msg.setBoxId(boxId);
            msg.setRewards(openBoxInfo.getRewards());
            msg.setBoxRound(boxRound);
        }
        msg.setGroupId(campDataData.getGroupId());
        msg.setCampId(campInfo.getCampId());

        CrossUnionCampWarGroupData groupData = CrossUnionCampWarLockGroupMgr.getGroupData(groupUnionData.getCenterGroupId(), groupUnionData.getGroupId());
        if (null != groupData) {
            for (CrossUnionCampWarGroupUnionData warGroupUnionData : groupData.getUnionMap().values()) {
                if (warGroupUnionData.getCampId() != groupUnionData.getCampId()) {
                    continue;
                }
                for (Long memberId : warGroupUnionData.getMermberPositionMap().keySet()) {
                    CrossUnionMember memberByUserId = CrossUnionMgr.getUnionMemberByUserId(memberId);
                    if (memberByUserId == null) {
                        continue;
                    }
                    MessageHelper.sendPacket(memberByUserId.getServerId(), memberId,
                            YanQuMessageUtils.buildMessage(ClientProtocol.U_UNION_CAMP_WAR_BOX_REFRESH, broadcastMsg));
                }
            }
        }
        return msg;
    }

    /**
     * 宝箱公告列表
     */
    public static UnionCampWar.UnionCampWarBoxNoticeListRespMsg.Builder getBoxNoticeListMsg(long userId) {
        UnionCampWar.UnionCampWarBoxNoticeListRespMsg.Builder msg = UnionCampWar.UnionCampWarBoxNoticeListRespMsg.newBuilder();
        msg.setRet(0);
        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

//        if (!ConfigMgr.getUnionCampWarConfig().isInRewardTime()) {
//            return msg;
//        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarCampInfo campInfo = warActivity.getCampInfo(groupUnionData.getCampId());
        if (null == campInfo) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_CAMP_INFO);
        }

        msg.addAllBoxNotice(campInfo.getBoxNoticeMsgList());

        return msg;
    }

    /**
     * 榜单领奖
     */
    public static UnionCampWar.UnionCampWarGetRankRewardRespMsg.Builder getUserRankReward(long userId) {
        UnionCampWar.UnionCampWarGetRankRewardRespMsg.Builder msg = UnionCampWar.UnionCampWarGetRankRewardRespMsg.newBuilder();
        msg.setRet(0);

//        if (!ConfigMgr.getUnionCampWarConfig().isInRewardTime()) {
//            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_REWARD_TIME);
//        }

        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        int winCampId = warActivity.getCampData().getWinCampId();

        UnionCampWarUserData warUserData = warActivity.getWarUserData(userId);
        if (warUserData == null) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_REWARD_SCORE);
        }
        if (warUserData.getGetReward() == 1){
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_BOX_ALREADY_OPEN);
        }
//        if (warUserData.getRank() == 0 || winCampId == 0) {
//            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_SETTLE_END);
//        }
        if (winCampId == 0) {
            return msg.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_SETTLE_END);
        }

//        String rewards = ConfigMgr.getUnionCampWarConfig().getRankRewards(warUserData.getCenterGroupId(), warUserData.getRank(), winCampId == warUserData.getCampId());
        String rewards = ConfigMgr.getUnionCampWarConfig().getCampRewards(warActivity.getCenterGroupId(), winCampId == warUserData.getCampId());

        warUserData.setGetReward(1);

        msg.setRank(warUserData.getRank());
        msg.setRewards(rewards);

        return msg;
    }

    /**
     * 聊天
     */
    public static void chat(long userId, WorldMessageProto.WorldMessageTempMsg chatMsg) {
        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return;
        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return;
        }

        WorldMessageProto.WorldMessageTempMsg broadChatMsg = warActivity.addChatMsg(chatMsg, userId);
        CrossUnionCampWarGroupData groupData = CrossUnionCampWarLockGroupMgr.getGroupData(groupUnionData.getCenterGroupId(), groupUnionData.getUnionId());
        if (null == groupData) {
            return;
        }


        for (CrossUnionCampWarGroupUnionData warGroupUnionData : groupData.getUnionMap().values()) {
            for (Long memberId : warGroupUnionData.getMermberPositionMap().keySet()) {
                CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(memberId);
                if (unionMember == null) {
                    continue;
                }
                if (broadChatMsg.getGroupId() != 0 && broadChatMsg.getGroupId() != warGroupUnionData.getCampId()) {
                    continue;
                }
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_WORLD_MESSAGE_SYNC, broadChatMsg);
                MessageHelper.sendPacket(unionMember.getServerId(), memberId, pbMsg);
            }
        }
    }

    /**
     * 拉取聊天
     */
    public static WorldMessageProto.WorldMessageListMsg getChatList(long userId, int messageType) {
        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return null;
        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return null;
        }

        return warActivity.getChatListMsg(messageType, userId);
    }

    /**
     * 获取两个阵营的商会名称列表
     */
    public static UnionCampWar.UnionCampWarUnionNameListRespMsg.Builder getUnionNameListMsg(long userId){
        UnionCampWar.UnionCampWarUnionNameListRespMsg.Builder builder = UnionCampWar.UnionCampWarUnionNameListRespMsg.newBuilder();
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (null == unionMember) {
            return builder;
        }
        CrossUnionCampWarGroupData groupData = null;
        if (ConfigMgr.getUnionCampWarConfig().isInRegistrationTime()) {//报名期
            groupData = CrossUnionCampWarLockGroupMgr.getGroupData(unionMember.getServerId(), unionMember.getUnionUid());
            if (null == groupData) {
                return builder;
            }
        } else {
            CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
            if (null == groupUnionData) {
                return builder;
            }

            groupData = CrossUnionCampWarLockGroupMgr.getGroupData(groupUnionData.getCenterGroupId(), groupUnionData.getGroupId());
            if (groupData == null) {
                return builder;
            }
        }

        for (CrossUnionCampWarGroupUnionData unionData : groupData.getUnionDataList()) {
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionData.getUnionId());
            if (unionInfo == null) {
                continue;
            }
            if (unionData.getCampId() == 1) {
                builder.addLeftUnionNames(unionInfo.getUnionName());
            } else {
                builder.addRightUnionNames(unionInfo.getUnionName());
            }
        }

        return builder;
    }

    /**
     * 点赞
     */
    public static UnionCampWar.UnionCampWarWorshipRespMsg.Builder worship(long userId) {
        UnionCampWar.UnionCampWarWorshipRespMsg.Builder builder = UnionCampWar.UnionCampWarWorshipRespMsg.newBuilder();
        builder.setRet(0);
        if (!ConfigMgr.getUnionCampWarConfig().isInRewardTime()) {
            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_REWARD_TIME);
        }
        CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
        if (null == groupUnionData) {
            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        UnionCampWarActivity warActivity = getWarActivity(groupUnionData);
        if (null == warActivity) {
            return builder.setRet(GameErrorCode.E_UNION_CAMP_WAR_NOT_QUALIFTCATION);
        }

        warActivity.addWorship(groupUnionData.getCampId());

        List<Integer> worshipCount = warActivity.getWorshipList();
        builder.addAllWorship(worshipCount);

        return builder;
    }

    /**
     * 热更专用
     */
    public static void hotFix(){

    }
}
